﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WebBrowserTest.AnalyzeResults;
using HtmlAgilityPack;


namespace WebBrowserTest.Analyzers
{
    public class WordDensityAnalyzer : AbstractAnalyzer
    {
        private List<string> _lookupTags = new List<string>();
        private int _allWordsCount = 0;
        private string _pageContent = string.Empty;
        private HtmlDocument _htmlDoc = new HtmlDocument();
        private string _keyword;
        private List<string> _pageWords;
        private string _pageText = string.Empty;
            
        #region Properties
        public List<string> LookupTags
        {
            get
            {
                return _lookupTags;
            }
            set
            {
                _lookupTags = value;
            }
        } 
        #endregion

        private WordDensityAnalyzer()
        {
        }

        public WordDensityAnalyzer(string keyword)
        {
            _keyword = keyword;
        }

        #region Private methods
        private List<string> GetAllPageWords()
        {
            _pageText = _htmlDoc.DocumentNode.InnerText.ToLower();
            _pageWords = _pageText.Split(' ').ToList();

            #region Old
            //string expr = @"(?<=^|>)[^><{}]+?(?=<|$)";
            //var matches = Regex.Matches(pageContent, expr);


            //foreach (Match match in matches)
            //{
            //    if (match.Groups[0].Value.Trim() != string.Empty)
            //    {

            //        pageWords.AddRange(match.Groups[0].Value.Split(' '));
            //    }
            //} 
            #endregion
            Regex noLetterExpr = new Regex("([^0-9A-Za-z]+)");
            _pageWords.RemoveAll(o=>!noLetterExpr.IsMatch(o));
            return _pageWords;
        }
      
        private int GetWordDensity(List<string> words, string keyword)
        {
            var matches = Regex.Matches(_pageText, keyword.ToLower());
            //return words.Where(o => o.ToLower().Contains(keyword.ToLower())).Count();
            return matches.Count;
        }

        private int GetWordsCount()
        {
            return _pageWords.Count;
        }
        private DensityAnalyzerResult GetWordDensity()
        {
            List<string> pageWords = GetAllPageWords();
            int wordDensity = GetWordDensity(pageWords, _keyword);
            return new DensityAnalyzerResult
                       {
                           Date = DateTime.Now,
                           Density = wordDensity,
                           Keyword = _keyword,
                           WordCount = GetWordsCount()
                       };
        } 
        #endregion

        public override AbstractAnalyzeResult GetAnalyseResult(string html, string url)
        {
            _pageContent = html;
            _htmlDoc.LoadHtml(html);
            return GetWordDensity();
        }
    }
}
